package com.hl.toolkit.server.bdpay;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class RefundQueryResult extends HttpServlet {

	private static final long serialVersionUID = 1L;
	private ServletConfig scon = null;
	private String service_code;
	private String sp_no;
	private String output_type;
	private String output_charset;
	private String version;
	private String sign_method;
	private String BFB_REFUND_QUERY_URL;

	public RefundQueryResult() {
		super();
	}
	/**
	 * 查询退款结果接口实现<br>
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @author zhaixuefei  2014/7/11
	 * @throws ServletException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String order_no =  "order_no="+request.getParameter("order_no");
		String sp_refund_no =  "sp_refund_no="+request.getParameter("sp_refund_no");
		String sign = null;
		if(sp_refund_no.endsWith("=")){
			String [] array = {service_code,sp_no,order_no,output_type,output_charset,version,sign_method};
			sign = "sign="+new BfbSdkComm().make_sign(array);
		}else{
			String [] array = {service_code,sp_no,sp_refund_no,order_no,output_type,output_charset,version,sign_method};
			sign = "sign="+new BfbSdkComm().make_sign(array);

		}
		

		//组装get参数
		String [] array1 = {order_no,output_charset,output_type,service_code,sign_method,sp_no,sp_refund_no,version,sign};
		String [] array2 = {order_no,output_charset,output_type,service_code,sign_method,sp_no,version,sign};
		
		//发起http请求
		HttpRequester requester = new HttpRequester();
		HttpRespons hr = requester.sendArrayParam(BFB_REFUND_QUERY_URL, "GET",sp_refund_no.endsWith("=")?array2:array1, null);
		Structs2Util.render(response, hr.getContent());
		System.out.print(hr.getContent());
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request,response);
	}
	//从配置获取参数
	public void init(ServletConfig config) throws ServletException {
		scon = config;
		service_code = "service_code="+scon.getServletContext().getInitParameter("BFB_REFUND_QUERY_INTERFACE_SERVICE_ID");
		sp_no =  "sp_no="+scon.getServletContext().getInitParameter("SP_NO");
		output_type =  "output_type="+scon.getServletContext().getInitParameter("BFB_INTERFACE_OUTPUT_FORMAT");
		output_charset =  "output_charset="+scon.getServletContext().getInitParameter("BFB_INTERFACE_OUTPUT_CHARSET");
		version =  "version="+scon.getServletContext().getInitParameter("BFB_INTERFACE_VERSION");
		sign_method =  "sign_method="+scon.getServletContext().getInitParameter("SIGN_METHOD_MD5");
		BFB_REFUND_QUERY_URL = scon.getServletContext().getInitParameter("BFB_REFUND_QUERY_URL");
	}

	public void destroy() {
		super.destroy();
	}
}
